package com.hubu.utils;

import java.util.ArrayList;
import java.util.List;



/**
 * @description:
 * @author: hubu
 * @time: 2023/3/31 11:03
 */
public class MathUtils {

    //Sigmoid
    public static double sigmoid(double value) {
        //Math.E=e;Math.Pow(a,b)=a^b
        double ey = Math.pow(Math.E, -value);
        double result = 1 / (1 + ey);
        return result;
    }

    //Sigmoid 求导
    public static double sigmoidDerivative(double value) {
        double A = sigmoid(value);
        double B = 1 - sigmoid(value);
        double result = A * B;
        return result;
    }

    public static List<Double> softmax(List<Double> list) {
        double countSimilarity = 0.0;

        ArrayList<Double> similarityList = new ArrayList<>();
        for (Double aDouble : list) {
            aDouble = aDouble * 10;
        }
        System.out.println(list);
//        for (Double aDouble : list) {
//            countSimilarity += Math.exp(aDouble * 10);
//
//        }
//        for (Double aDouble : list) {
//            similarityList.add(Math.exp(aDouble * 10) / countSimilarity);
//        }

        double mean = mean(list);
        double stdDev = stdDev(list, mean);

        List<Double> doubles = zScoreNormalization(list);
        System.out.println(doubles);
        return doubles;
//        System.out.println(similarityList);
//        return similarityList;
    }

    public static double mean(List<Double> list) {
        double sum = 0.0;
        for (Double value : list) {
            sum += value;
        }
        return sum / list.size();
    }
    public static List<Double> zScoreNormalization(List<Double> list) {
        //ArrayList<Double> doubleArrayList = new ArrayList<>();
        double mean = mean(list);
        double stdDev = stdDev(list, mean);
        for (Double aDouble : list) {
            aDouble = (aDouble - mean) / stdDev;
        }
        return list;
    }
    public static double stdDev(List<Double> list, Double mean) {
        Double sum = 0.0;
        for (Double value : list) {
            sum += Math.pow(value - mean, 2);
        }
        double variance = sum / (list.size() - 1);
        return Math.sqrt(variance);
    }

}
